<?php

Class BudgetsController extends AppController {

    public $name = 'Budgets';

    public function add($follow_id) {
        $this->set('follow_id', $follow_id);
        if (empty($this->data)) {
            $this->Budget->MonitoringActivity->virtualFields = array(
                'name' => 'CONCAT(MonitoringActivity.nombre, " (", MonitoringActivity.tipo,")")'
            );
            $this->set('monitoringActivities', $this->Budget->MonitoringActivity->find('list', array('fields' => array('id', 'name'), 'order' => array('MonitoringActivity.nombre ASC'))));
        } else {
            if ($this->data['Budget']['cantidad'] >= 1 and ($this->data['Budget']['cantidad'] * $this->data['Budget']['valor_unitario']) == ($this->data['Budget']['cofinanciacion_incoder'] + $this->data['Budget']['cofinaciacion_comunidad'] + $this->data['Budget']['contapartida_certificada'] + $this->data['Budget']['otra_contrapartida'])) {
                if ($this->Budget->save($this->data)) {
                    $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                    $this->redirect(array('controller' => 'Budgets', 'action' => 'index', $follow_id));
                } else {
                    $this->Session->setFlash('Error Guardando datos');
                }
            } else {
                $this->Session->setFlash('El valor unitario por la cantidad debe ser igual a la suma de las cofinanciaciones. Por favor verificar la información.', 'flash_custom');
                $this->Budget->MonitoringActivity->virtualFields = array(
                    'name' => 'CONCAT(MonitoringActivity.nombre, " (", MonitoringActivity.tipo,")")'
                );
                $this->set('monitoringActivities', $this->Budget->MonitoringActivity->find('list', array('fields' => array('id', 'name'), 'order' => array('MonitoringActivity.nombre ASC'))));
            }
        }
    }

    public function edit($id) {
        $this->Budget->MonitoringActivity->virtualFields = array(
            'name' => 'CONCAT(MonitoringActivity.nombre, " (", MonitoringActivity.tipo,")")'
        );
        $this->set('monitoringActivities', $this->Budget->MonitoringActivity->find('list', array('fields' => array('id', 'name'), 'order' => array('MonitoringActivity.nombre ASC'))));
        $this->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Budget->find('first', array('conditions' => array('Budget.id' => $id)));
        } else {
            if ($this->data['Budget']['cantidad'] >= 1 and ($this->data['Budget']['cantidad'] * $this->data['Budget']['valor_unitario']) == ($this->data['Budget']['cofinanciacion_incoder'] + $this->data['Budget']['cofinaciacion_comunidad'] + $this->data['Budget']['contapartida_certificada'] + $this->data['Budget']['otra_contrapartida'])) {
                if ($this->Budget->save($this->data)) {
                    $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                    $this->redirect(array('controller' => 'Budgets', 'action' => 'index', $this->data['Budget']['follow_id']));
                } else {
                    $this->Session->setFlash('Error editando datos');
                }
            } else {
                $this->Session->setFlash('El valor unitario por la cantidad debe ser igual a la suma de las cofinanciaciones. Por favor verificar la información.', 'flash_custom');
                $this->Budget->MonitoringActivity->virtualFields = array(
                    'name' => 'CONCAT(MonitoringActivity.nombre, " (", MonitoringActivity.tipo,")")'
                );
                $this->data = $this->Budget->find('first', array('conditions' => array('Budget.id' => $id)));
            }
        }
    }

    public function edit_calendario($id) {
        $this->set('monitoringActivities', $this->Budget->MonitoringActivity->find('list', array('fields' => array('id', 'nombre'))));
        $this->Budget->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->Budget->find('first', array('conditions' => array('Budget.id' => $id)));
        } else {
            if ($this->data['Budget']['mes1'] > -1 and $this->data['Budget']['mes2'] > -1 and $this->data['Budget']['mes3'] > -1 and $this->data['Budget']['mes4'] > -1 and $this->data['Budget']['mes5'] > -1 and $this->data['Budget']['mes6'] > -1 and
                    $this->data['Budget']['mes7'] > -1 and $this->data['Budget']['mes8'] > -1 and $this->data['Budget']['mes9'] > -1 and $this->data['Budget']['mes10'] > -1 and $this->data['Budget']['mes11'] > -1 and $this->data['Budget']['mes12'] > -1 and
                    ($this->data['Budget']['mes1'] + $this->data['Budget']['mes2'] + $this->data['Budget']['mes3'] + $this->data['Budget']['mes4'] + $this->data['Budget']['mes5'] + $this->data['Budget']['mes6']
                    + $this->data['Budget']['mes7'] + $this->data['Budget']['mes8'] + $this->data['Budget']['mes9'] + $this->data['Budget']['mes10'] + $this->data['Budget']['mes11'] + $this->data['Budget']['mes12']) == ($this->data['Budget']['valor_unitario'] * $this->data['Budget']['cantidad'])) {
                if ($this->Budget->save($this->data)) {
                    $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                    $this->redirect(array('controller' => 'Budgets', 'action' => 'index', $this->data['Budget']['follow_id']));
                } else {
                    $this->Session->setFlash('Error editando datos');
                }
            } else {
                $this->Session->setFlash('La suma de valores ingresados ' . ($this->data['Budget']['mes1'] + $this->data['Budget']['mes2'] + $this->data['Budget']['mes3'] + $this->data['Budget']['mes4'] + $this->data['Budget']['mes4'] + $this->data['Budget']['mes6'] + $this->data['Budget']['mes7'] + $this->data['Budget']['mes8'] + $this->data['Budget']['mes9'] + $this->data['Budget']['mes10'] + $this->data['Budget']['mes11'] + $this->data['Budget']['mes12']) . ' es diferente a la del plan de inversión ' . ($this->data['Budget']['valor_unitario'] * $this->data['Budget']['cantidad']), 'flash_custom');
                $this->data = $this->Budget->find('first', array('conditions' => array('Budget.id' => $id)));
            }
        }
    }

    public function print_budget($follow_id) {
        $this->layout = "ajax";
        $this->set('follow_id', $follow_id);
        $proyect_id = $this->Session->read('proy_id');
        $this->set('proyect_id', $proyect_id);
        $Budgets = $this->Budget->find('all', array('recursive' => 0, 'order' => array('Budget.monitoring_activity_id ASC'), 'conditions' => array('Budget.follow_id' => $follow_id)));
        $this->set('Budgets', $Budgets);
        $final_evaluation_id = $this->Budget->Follow->field('Follow.final_evaluation_id', array('Follow.id' => $follow_id));
        $this->set('final_evaluation_id', $final_evaluation_id);

        App::Import('Model', 'FinalEvaluation');
        $fe = new FinalEvaluation();
        $fe->recursive = -1;

        $familias_habilitadas = $fe->field('familias_habilitadas', array('FinalEvaluation.id' => $final_evaluation_id));

        App::Import('Model', 'Candidate');
        $can = new Candidate();
        $can->recursive = -1;

        $fam_campesinas = $can->find('count', array('conditions' => array('Candidate.proyect_id' => $this->Session->read('proy_id'), 'Candidate.candidate_id' => 0, 'Candidate.renuncio' => 0, 'Candidate.estado_filtro' => array(1, 5, 6), 'Candidate.tipo' => array(2, 6), 'Candidate.jerarquia !=' => 0, 'Candidate.jerarquia <=' => $familias_habilitadas)));
        $fam_desplazadas = $can->find('count', array('conditions' => array('Candidate.proyect_id' => $this->Session->read('proy_id'), 'Candidate.candidate_id' => 0, 'Candidate.renuncio' => 0, 'Candidate.estado_filtro' => array(1, 5, 6), 'Candidate.tipo' => array(4, 7), 'Candidate.jerarquia !=' => 0, 'Candidate.jerarquia <=' => $familias_habilitadas)));
        $fam_profesional = $can->find('count', array('conditions' => array('Candidate.proyect_id' => $this->Session->read('proy_id'), 'Candidate.candidate_id' => 0, 'Candidate.renuncio' => 0, 'Candidate.estado_filtro' => array(1, 5, 6), 'Candidate.tipo' => array(5, 8), 'Candidate.jerarquia !=' => 0, 'Candidate.jerarquia <=' => $familias_habilitadas)));
        if ($fam_campesinas > 0)
            $this->set('fam_campesinas', $fam_campesinas);
        if ($fam_desplazadas > 0)
            $this->set('fam_desplazadas', $fam_desplazadas);
        if ($fam_profesional > 0)
            $this->set('fam_profesional', $fam_profesional);

        $this->loadModel('Resolution');

        $resolucion = $this->Resolution->find('first', array('recursive' => -1, 'conditions' => array('Resolution.proyect_id' => $proyect_id), 'order' => array('Resolution.id DESC')));
        $this->set('resolucion', $resolucion);
    }

    public function print_calendar($follow_id) {
        $this->layout = "ajax";
        $proyect_id = $this->Session->read('proy_id');
        $this->set('proyect_id', $proyect_id);
        $this->set('follow_id', $follow_id);

        $Budgets = $this->Budget->find('all', array('conditions' => array('Budget.follow_id' => $follow_id), 'recursive' => 0, 'order' => array('Budget.monitoring_activity_id ASC'), 'fields' => array('Budget.*', 'MonitoringActivity.nombre', 'MonitoringActivity.tipo')));
        $this->set('Budgets', $Budgets);
    }

    public function index($follow_id) {
        $this->layout = "ajax";
        $this->set('follow_id', $follow_id);
        $this->loadModel('Follow');
        $proyect_id = $this->Session->read('proy_id');
        $this->set('proyect_id', $proyect_id);
        $this->set('cerrado', $this->Follow->field('cerrado', array('Follow.id' => $follow_id)));
        $budgets = $this->Budget->find('all', array('recursive' => 0, 'order' => array('Budget.monitoring_activity_id ASC'), 'conditions' => array('Budget.follow_id' => $follow_id)));
        $this->set('Budgets', $budgets);
        $this->set('final_evaluation_id', $this->Budget->Follow->field('Follow.final_evaluation_id', array('Follow.id' => $follow_id)));
    }

    public function delete($id, $follow_id) {
        if ($this->Budget->delete($id)) {
            $this->Session->setFlash('Registro eliminado correctamente', 'flash_custom');
            $this->redirect(array('controller' => 'Budgets', 'action' => 'index', $follow_id));
        } else {
            $this->Session->setFlash('Error eliminando los datos');
        }
    }

}

?>